home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / manage / snmp / mit / bsd / if.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-17  |  10.8 KB  |  513 lines

  1.  
  2. /*
  3.  *    $Header: if.c,v 3.0 91/05/17 16:14:24 jrd Rel $
  4.  *    Author: J. Davin
  5.  *    Copyright 1988, 1989, Massachusetts Institute of Technology
  6.  *    See permission and disclaimer notice in file "notice.h"
  7.  */
  8.  
  9. #include    <notice.h>
  10.  
  11. #include    <sys/types.h>
  12. #include    <nlist.h>
  13. #include    <sys/mbuf.h>
  14. #include    <sys/socket.h>
  15. #include    <netinet/in.h>
  16. #define        KERNEL
  17. #include    <net/if.h>
  18. #undef        KERNEL
  19. #include    <stdio.h>
  20.  
  21. #include    <ctypes.h>
  22. #include    <error.h>
  23. #include    <debug.h>
  24. #include    <local.h>
  25. #include    <mix.h>
  26. #include    <mis.h>
  27. #include    <asn.h>
  28.  
  29. #include    <kmem.h>
  30. #include    <if.h>
  31.  
  32. #define        ifMaxNameSize        (16)
  33.  
  34. typedef        struct            IfRecTag {
  35.  
  36.             struct IfRecTag        *ifRecNext;
  37.         CUnswType        ifRecAddr;
  38.         CIntlType        ifRecIndex;
  39.         CByteType        ifRecName [ ifMaxNameSize ];
  40.         AsnLengthType        ifRecNameLen;
  41.         struct    ifnet        ifRecData;
  42.  
  43.         }            IfRecType;
  44.  
  45. typedef        IfRecType        *IfRecPtrType;
  46.  
  47. typedef        AsnIdType        (*IfGetFnType) ();
  48. typedef        MixStatusType        (*IfSetFnType) ();
  49.  
  50. typedef        struct            IfCookieTag {
  51.  
  52.             MixNamePtrType        ifCookieName;
  53.         MixLengthType        ifCookieNameLen;
  54.             IfGetFnType        ifCookieGetFn;
  55.             IfSetFnType        ifCookieSetFn;
  56.  
  57.                 }            IfCookieType;
  58.  
  59. typedef        IfCookieType        *IfCookiePtrType;
  60.  
  61. #define    ifVarDefn(a,b,c,d)    \
  62.         { (MixNamePtrType) (a), (MixLengthType) (b), (c), (d) },
  63.  
  64. static        IfRecPtrType        ifChainHead;
  65. static        CUnswType        ifChainAddr;
  66.  
  67. static    IfRecPtrType    ifLookUp (head, item)
  68.  
  69. IfRecPtrType        head;
  70. CIntfType        item;
  71.  
  72. {
  73.     IfRecPtrType            p;
  74.     CIntfType            i;
  75.     static    CIntfType        cachedItem    =    0;
  76.     static    IfRecPtrType        cachedEntry;
  77.  
  78.         if (item <= 0) {
  79.         return ((IfRecPtrType) 0);
  80.     }
  81.     else if (item == cachedItem) {
  82.         return (cachedEntry);
  83.     }
  84.     for (i = item; (i > 1) && (head != (IfRecPtrType) 0); i--) {
  85.         head = head->ifRecNext;
  86.     }
  87.     if (head != (IfRecPtrType) 0) {
  88.         cachedItem = item;
  89.         cachedEntry = head;
  90.     }
  91.     return (head);
  92. }
  93.  
  94. static    IfRecPtrType    ifChainFree (head)
  95.  
  96. IfRecPtrType        head;
  97.  
  98. {
  99.     if (head != (IfRecPtrType) 0) {
  100.         head->ifRecNext = ifChainFree (head->ifRecNext);
  101.         (void) free ((char *) head);
  102.     }
  103.     return ((IfRecPtrType) 0);
  104. }
  105.  
  106. static    CBoolType    ifEntryRefresh (head)
  107.  
  108. IfRecPtrType        head;
  109.  
  110. {
  111.     CCharType        nameBuf [ ifMaxNameSize ];
  112.  
  113.     if (head != (IfRecPtrType) 0) {
  114.         if (kmemRead ((CBytePtrType) & head->ifRecData,
  115.                 (CIntfType) sizeof (head->ifRecData),
  116.             head->ifRecAddr) != (CIntfType) sizeof
  117.                 (head->ifRecData)) {
  118.             return (FALSE);
  119.         }
  120.         else if (kmemRead ((CBytePtrType) nameBuf,
  121.                 (CIntfType) sizeof (nameBuf),
  122.             (CUnswType) head->ifRecData.if_name) != (CIntfType) sizeof
  123.                 (nameBuf)) {
  124.             return (FALSE);
  125.         }
  126.         else {
  127.             *(nameBuf + sizeof (nameBuf)) = (CCharType) 0;
  128.             (void) sprintf ((char *) head->ifRecName,
  129.                     "%s%d", (char *) nameBuf,
  130.                 head->ifRecData.if_unit);
  131.             head->ifRecNameLen =
  132.                 (AsnLengthType) strlen ((char *) head->ifRecName);
  133.             return (TRUE);
  134.         }
  135.     }
  136.     else {
  137.         return (FALSE);
  138.     }
  139. }
  140.  
  141. static    IfRecPtrType    ifChainRefresh (location, index)
  142.  
  143. CUnswType        location;
  144. CIntlType        index;
  145.  
  146. {
  147.     IfRecPtrType            entry;
  148.  
  149.     entry = (IfRecPtrType) malloc ((unsigned) sizeof (*entry));
  150.     if (entry == (IfRecPtrType) 0) {
  151.         return (entry);
  152.     }
  153.     entry->ifRecAddr = location;
  154.  
  155.     if (ifEntryRefresh (entry) != TRUE) {
  156.         (void) free ((char *) entry);
  157.         return ((IfRecPtrType) 0);
  158.     }
  159.     entry->ifRecIndex = index;
  160.     entry->ifRecNext = ifChainRefresh (entry->ifRecData.if_next,
  161.                 index + 1);
  162.     return (entry);
  163. }
  164.  
  165. static    IfRecPtrType    ifTableRefresh (location)
  166.  
  167. CUnswType        location;
  168.  
  169. {
  170.     CUnswType        first;
  171.  
  172.     if (kmemRead ((CBytePtrType) & first,
  173.         (CIntfType) sizeof (first), location) !=
  174.         (CIntfType) sizeof (first)) {
  175.         return ((IfRecPtrType) 0);
  176.     }
  177.     else {
  178.         return (ifChainRefresh (first, (CIntlType) 1));
  179.     }
  180. }
  181.  
  182. static    AsnIdType    ifRetrieve (mix, item)
  183.  
  184. IfCookiePtrType        mix;
  185. CIntfType        item;
  186.  
  187. {
  188.     IfRecPtrType        entry;
  189.  
  190.     if ((entry = ifLookUp (ifChainHead, item)) ==
  191.             (IfRecPtrType) 0) {
  192.         return ((AsnIdType) 0);
  193.     }
  194.     else if (ifEntryRefresh (entry)) {
  195.         return ((*mix->ifCookieGetFn) (entry));
  196.     }
  197.     else {
  198.         return ((AsnIdType) 0);
  199.     }
  200. }
  201.  
  202. static    MixStatusType    ifRelease (cookie)
  203.  
  204. MixCookieType        cookie;
  205.  
  206. {
  207.     cookie = cookie;
  208.     return (smpErrorGeneric);
  209. }
  210.  
  211. static    MixStatusType    ifCreate (cookie, name, namelen, asn)
  212.  
  213. MixCookieType        cookie;
  214. MixNamePtrType        name;
  215. MixLengthType        namelen;
  216. AsnIdType        asn;
  217.  
  218. {
  219.     cookie = cookie;
  220.     name = name;
  221.     namelen = namelen;
  222.     asn = asn;
  223.     return (smpErrorGeneric);
  224. }
  225.  
  226. static    MixStatusType    ifDestroy (cookie, name, namelen)
  227.  
  228. MixCookieType        cookie;
  229. MixNamePtrType        name;
  230. MixLengthType        namelen;
  231.  
  232. {
  233.     cookie = cookie;
  234.     name = name;
  235.     namelen = namelen;
  236.     return (smpErrorGeneric);
  237. }
  238.  
  239. static    AsnIdType    ifGet (cookie, name, namelen)
  240.  
  241. MixCookieType        cookie;
  242. MixNamePtrType        name;
  243. MixLengthType        namelen;
  244.  
  245. {
  246.     if (namelen != (MixLengthType) 1) {
  247.         return ((AsnIdType) 0);
  248.     }
  249.     else {
  250.         return (ifRetrieve ((IfCookiePtrType) cookie,
  251.                         (CIntfType) *name));
  252.     }
  253. }
  254.  
  255. static    MixStatusType    ifSet (cookie, name, namelen, asn)
  256.  
  257. MixCookieType        cookie;
  258. MixNamePtrType        name;
  259. MixLengthType        namelen;
  260. AsnIdType        asn;
  261.  
  262. {
  263.     cookie = cookie;
  264.     name = name;
  265.     namelen = namelen;
  266.     asn = asn;
  267.     return (smpErrorGeneric);
  268. }
  269.  
  270. static    AsnIdType    ifNext (cookie, name, namelenp)
  271.  
  272. MixCookieType        cookie;
  273. MixNamePtrType        name;
  274. MixLengthPtrType    namelenp;
  275.  
  276. {
  277.     CIntfType        item;
  278.  
  279.     if (*namelenp == (MixLengthType) 0) {
  280.         *namelenp = (MixLengthType) 1;
  281.         *name = (MixNameType) 1;
  282.         return (ifRetrieve ((IfCookiePtrType) cookie, (CIntfType) 1));
  283.     }
  284.     else {
  285.         item = (CIntfType) *name;
  286.         *namelenp = (MixLengthType) 1;
  287.         *name = (MixNameType) (++item);
  288.         return (ifRetrieve ((IfCookiePtrType) cookie, item));
  289.     }
  290. }
  291.  
  292. static    MixOpsType    ifOps = {
  293.  
  294.             ifRelease,
  295.             ifCreate,
  296.             ifDestroy,
  297.             ifNext,
  298.             ifGet,
  299.             ifSet
  300.  
  301.             };
  302.  
  303. static    MixStatusType        ifSetUnsupported ()
  304.  
  305. {
  306.     return (smpErrorNone);
  307. }
  308.  
  309. static    AsnIdType        ifGetUnsupported (entry)
  310.  
  311. IfRecPtrType            entry;
  312.  
  313. {
  314.     return (asnIntl (asnClassUniversal, (AsnTagType) 2,
  315.             (CIntlType) 0));
  316. }
  317.  
  318. static    AsnIdType        ifIndexGet (entry)
  319.  
  320. IfRecPtrType            entry;
  321.  
  322. {
  323.     return (asnIntl (asnClassUniversal, (AsnTagType) 2,
  324.             entry->ifRecIndex));
  325. }
  326.  
  327. static    AsnIdType        ifDescrGet (entry)
  328.  
  329. IfRecPtrType            entry;
  330.  
  331. {
  332.     return (asnOctetString (asnClassUniversal, (AsnTagType) 4,
  333.             entry->ifRecName, entry->ifRecNameLen));
  334. }
  335.  
  336. static    AsnIdType        ifMtuGet (entry)
  337.  
  338. IfRecPtrType            entry;
  339.  
  340. {
  341.     return (asnIntl (asnClassUniversal, (AsnTagType) 2,
  342.             (CIntlType) entry->ifRecData.if_mtu));
  343. }
  344.  
  345. static    AsnIdType        ifSpeedGet (entry)
  346.  
  347. IfRecPtrType            entry;
  348.  
  349. {
  350.     return (asnUnsl (asnClassApplication, (AsnTagType) 2,
  351.             (CUnslType) 0));
  352. }
  353.  
  354. static    AsnIdType        ifPhysAddressGet (entry)
  355.  
  356. IfRecPtrType            entry;
  357.  
  358. {
  359.     return (asnOctetString (asnClassUniversal, (AsnTagType) 4,
  360.             entry->ifRecName, (AsnLengthType) 0));
  361. }
  362.  
  363. static    AsnIdType        ifAdminStatusGet (entry)
  364.  
  365. IfRecPtrType            entry;
  366.  
  367. {
  368.     CIntlType        result;
  369.  
  370.     result = (entry->ifRecData.if_flags & IFF_UP) ? 1 : 2;
  371.     return (asnIntl (asnClassUniversal, (AsnTagType) 2,
  372.             result));
  373. }
  374.  
  375. static    AsnIdType        ifLastChangeGet (entry)
  376.  
  377. IfRecPtrType            entry;
  378.  
  379. {
  380.     return (asnIntl (asnClassApplication, (AsnTagType) 3,
  381.             (CIntlType) 0));
  382. }
  383.  
  384. static    AsnIdType        ifInOctetsGet (entry)
  385.  
  386. IfRecPtrType            entry;
  387.  
  388. {
  389.     return (asnUnsl (asnClassApplication, (AsnTagType) 1,
  390.             (CUnslType) 0));
  391. }
  392.  
  393. static    AsnIdType        ifInUcastPktsGet (entry)
  394.  
  395. IfRecPtrType            entry;
  396.  
  397. {
  398.     return (asnUnsl (asnClassApplication, (AsnTagType) 1,
  399.             (CUnslType) entry->ifRecData.if_ipackets));
  400. }
  401.  
  402. static    AsnIdType        ifInErrorsGet (entry)
  403.  
  404. IfRecPtrType            entry;
  405.  
  406. {
  407.     return (asnUnsl (asnClassApplication, (AsnTagType) 1,
  408.             (CUnslType) entry->ifRecData.if_ierrors));
  409. }
  410.  
  411. static    AsnIdType        ifOutUcastPktsGet (entry)
  412.  
  413. IfRecPtrType            entry;
  414.  
  415. {
  416.     return (asnUnsl (asnClassApplication, (AsnTagType) 1,
  417.             (CUnslType) entry->ifRecData.if_opackets));
  418. }
  419.  
  420. static    AsnIdType        ifOutDiscardsGet (entry)
  421.  
  422. IfRecPtrType            entry;
  423.  
  424. {
  425.     return (asnUnsl (asnClassApplication, (AsnTagType) 1,
  426.             (CUnslType) entry->ifRecData.if_snd.ifq_drops));
  427. }
  428.  
  429. static    AsnIdType        ifOutErrorsGet (entry)
  430.  
  431. IfRecPtrType            entry;
  432.  
  433. {
  434.     return (asnUnsl (asnClassApplication, (AsnTagType) 1,
  435.             (CUnslType) entry->ifRecData.if_oerrors));
  436. }
  437.  
  438. static    AsnIdType        ifOutQLenGet (entry)
  439.  
  440. IfRecPtrType            entry;
  441.  
  442. {
  443.     return (asnUnsl (asnClassApplication, (AsnTagType) 2,
  444.             (CUnslType) entry->ifRecData.if_snd.ifq_len));
  445. }
  446.  
  447. static    AsnIdType        ifSpecificGet (entry)
  448.  
  449. IfRecPtrType            entry;
  450.  
  451. {
  452.     return (asnObjectId (asnClassUniversal, (AsnTagType) 6,
  453.             (CBytePtrType) "\0", (AsnLengthType) 1));
  454. }
  455.  
  456. #define        ifTypeGet        ifGetUnsupported
  457. #define        ifOperStatusGet        ifAdminStatusGet
  458. #define        ifInNUcastPktsGet    ifInOctetsGet
  459. #define        ifInDiscardsGet        ifInOctetsGet
  460. #define        ifInUnknownProtosGet    ifInOctetsGet
  461. #define        ifOutOctetsGet        ifInOctetsGet
  462. #define        ifOutNUcastPktsGet    ifInOctetsGet
  463.  
  464. static    IfCookieType        ifVarTable [] = {
  465.  
  466.     ifVarDefn ("\53\6\1\2\1\2\2\1\1", 9, ifIndexGet, ifSetUnsupported)
  467.     ifVarDefn ("\53\6\1\2\1\2\2\1\2", 9, ifDescrGet, ifSetUnsupported)
  468.     ifVarDefn ("\53\6\1\2\1\2\2\1\3", 9, ifTypeGet, ifSetUnsupported)
  469.     ifVarDefn ("\53\6\1\2\1\2\2\1\4", 9, ifMtuGet, ifSetUnsupported)
  470.     ifVarDefn ("\53\6\1\2\1\2\2\1\5", 9, ifSpeedGet, ifSetUnsupported)
  471.     ifVarDefn ("\53\6\1\2\1\2\2\1\6", 9, ifPhysAddressGet, ifSetUnsupported)
  472.     ifVarDefn ("\53\6\1\2\1\2\2\1\7", 9, ifAdminStatusGet, ifSetUnsupported)
  473.     ifVarDefn ("\53\6\1\2\1\2\2\1\10", 9, ifOperStatusGet, ifSetUnsupported)
  474.     ifVarDefn ("\53\6\1\2\1\2\2\1\11", 9, ifLastChangeGet, ifSetUnsupported)
  475.     ifVarDefn ("\53\6\1\2\1\2\2\1\12", 9, ifInOctetsGet, ifSetUnsupported)
  476.     ifVarDefn ("\53\6\1\2\1\2\2\1\13", 9, ifInUcastPktsGet, ifSetUnsupported)
  477.     ifVarDefn ("\53\6\1\2\1\2\2\1\14", 9, ifInNUcastPktsGet, ifSetUnsupported)
  478.     ifVarDefn ("\53\6\1\2\1\2\2\1\15", 9, ifInDiscardsGet, ifSetUnsupported)
  479.     ifVarDefn ("\53\6\1\2\1\2\2\1\16", 9, ifInErrorsGet, ifSetUnsupported)
  480.     ifVarDefn ("\53\6\1\2\1\2\2\1\17", 9, ifInUnknownProtosGet, ifSetUnsupported)
  481.     ifVarDefn ("\53\6\1\2\1\2\2\1\20", 9, ifOutOctetsGet, ifSetUnsupported)
  482.     ifVarDefn ("\53\6\1\2\1\2\2\1\21", 9, ifOutUcastPktsGet, ifSetUnsupported)
  483.     ifVarDefn ("\53\6\1\2\1\2\2\1\22", 9, ifOutNUcastPktsGet, ifSetUnsupported)
  484.     ifVarDefn ("\53\6\1\2\1\2\2\1\23", 9, ifOutDiscardsGet, ifSetUnsupported)
  485.     ifVarDefn ("\53\6\1\2\1\2\2\1\24", 9, ifOutErrorsGet, ifSetUnsupported)
  486.     ifVarDefn ("\53\6\1\2\1\2\2\1\25", 9, ifOutQLenGet, ifSetUnsupported)
  487.     ifVarDefn ("\53\6\1\2\1\2\2\1\26", 9, ifSpecificGet, ifSetUnsupported)
  488.  
  489.     ifVarDefn (0, 0, ifGetUnsupported, ifSetUnsupported)
  490. };
  491.  
  492. CVoidType        ifInit ()
  493.  
  494. {
  495.     CIntfType            i;
  496.     struct        nlist        nl [ 4 ];
  497.     IfCookiePtrType            cp;
  498.  
  499.     nl [ 0 ].n_name = "_ifnet";
  500.     nl [ 1 ].n_name = (char *) 0;
  501.     if (nlist ("/vmunix", nl) != 0) {
  502.         return;
  503.     }
  504.     ifChainAddr = (CUnslType) nl [ 0 ].n_value;
  505.     ifChainHead = ifTableRefresh (ifChainAddr);
  506.  
  507.     for (cp = ifVarTable; cp->ifCookieName != (MixNamePtrType) 0;
  508.                 cp++) {
  509.         (void) misExport (cp->ifCookieName, cp->ifCookieNameLen,
  510.         & ifOps, (MixCookieType) cp);
  511.     }
  512. }
  513.